﻿Imports System.Data.OleDb

Public Class propertyDetail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If IsPostBack = False Then
            If Request.Params("id") = "" Then Response.Redirect("properties.aspx")

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("gozoFarmhouses").ConnectionString)
            Dim SqlString As String = "SELECT * FROM Comments WHERE Property_fkid = @f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text

            cmd.Parameters.AddWithValue("@f1", Request.Params("id"))

            oleDbConn.Open()
            Dim commentsReader = cmd.ExecuteReader()

            ListView3.DataSource = commentsReader
            ListView3.DataBind()

        End If

    End Sub

    Protected Sub addComment_Click(sender As Object, e As EventArgs) Handles addComment.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("gozoFarmhouses").ConnectionString)
        Dim SqlString As String = "Insert into Comments(Author, Comment, Comment_Date, Property_fkid) Values (@f1, @f2, @f3, @f4)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text

        Dim currentTime = System.DateTime.Now

        cmd.Parameters.AddWithValue("@f1", author.Text)
        cmd.Parameters.AddWithValue("@f2", comment.Value)
        cmd.Parameters.AddWithValue("@f2", currentTime.Date)
        cmd.Parameters.AddWithValue("@f4", Request.Params("id"))
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

        Dim SqlString2 As String = "SELECT * FROM Comments WHERE Property_fkid = @f5"
        Dim cmd2 As OleDbCommand = New OleDbCommand(SqlString2, oleDbConn)
        cmd2.CommandType = CommandType.Text
        cmd2.Parameters.AddWithValue("@f5", Request.Params("id"))
        Dim commentsReader = cmd2.ExecuteReader()

        ListView3.DataSource = commentsReader
        ListView3.DataBind()

        author.Text = ""
        comment.Value = ""
    End Sub


    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim oleDbConn3 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("gozoFarmhouses").ConnectionString)
        Dim SqlString3 As String = "Insert into PropertyRating(PropertyID, Rating) Values (@f1, @f2)"
        Dim cmd3 As OleDbCommand = New OleDbCommand(SqlString3, oleDbConn3)
        cmd3.CommandType = CommandType.Text

        cmd3.Parameters.AddWithValue("@f1", Request.Params("id"))
        cmd3.Parameters.AddWithValue("@f2", Rating1.CurrentRating)
        oleDbConn3.Open()
        cmd3.ExecuteNonQuery()
    End Sub
End Class